﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.FAQ.Components;
using ASPNETVN.PORTAL.Components.Share.Controls;
using ASPNETVN.PORTAL.Components.Share;

namespace ASPNETVN.PORTAL.Modules.FAQ.Controls
{
    public partial class ListQuestion : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                this.GridDataBinding();
            }
        }

        protected void gvItem_DataBound(object sender, EventArgs e)
        {
            if (gvItem.Rows.Count > 0)
            {
                // Retrieve the pager row.
                GridViewRow bottomPagerRow = gvItem.BottomPagerRow;
                GridViewRow topPagerRow = gvItem.TopPagerRow;

                // Retrieve the DropDownList and Label controls from the row.
                DropDownList topPageList = (DropDownList)topPagerRow.Cells[0].FindControl("PageDropDownList");
                DropDownList bottomPageList = (DropDownList)bottomPagerRow.Cells[0].FindControl("PageDropDownList");
                Label topPageLabel = (Label)topPagerRow.Cells[0].FindControl("PageLabel");
                Label bottomPageLabel = (Label)bottomPagerRow.Cells[0].FindControl("PageLabel");
                Label totalRecord = (Label)topPagerRow.Cells[0].FindControl("TotalRecordLabel");
                totalRecord.Text = ((DataView)gvItem.DataSource).Count.ToString("N0");
                if (topPageList != null && bottomPageList != null)
                {
                    for (int i = 0; i < gvItem.PageCount; i++)
                    {
                        int pageNumber = i + 1;
                        ListItem item = new ListItem(pageNumber.ToString());
                        if (i == gvItem.PageIndex)
                        {
                            item.Selected = true;
                        }

                        topPageList.Items.Add(item);
                        bottomPageList.Items.Add(item);
                    }
                }

                if (topPageLabel != null && bottomPageLabel != null)
                {
                    topPageLabel.Text = bottomPageLabel.Text = " / " + gvItem.PageCount.ToString();
                }
            }
        }

        protected void PageDropDownList_SelectedIndexChanged(Object sender, EventArgs e)
        {
            DropDownList ddl = (DropDownList)sender;
            gvItem.PageIndex = ddl.SelectedIndex;
            this.GridDataBinding();            
        }

        private void GridDataBinding()
        {
            DataView dv;
            string where = "1=1";
            if (!ASPNETVN.PORTAL.Components.Security.PortalSecurity.HasManageAuthorization(this.ModuleID))
            {
                where += "and Status<>";
                where += FAQStatus.NotApprove;
            }
            if (txtSearch.Text.Trim() != "")
            {
                where += " and Title like N'%" + txtSearch.Text.Trim() + "%' ";
                where += " or QuesContent like N'%" + txtSearch.Text.Trim() + "%' ";
                where += " or Name like N'%" + txtSearch.Text.Trim() + "%' ";
            }
            dv = Components.Item.SelectDynamic(where, " ID desc").Tables[0].DefaultView; 
            gvItem.DataSource = dv;
            gvItem.DataBind();            
        }

        protected string GetEditURL(int itemID)
        {
            return HttpUrlBuilder.BuildAddEditUrl(this.ModuleID, itemID, "AddItem", "");
        }

        protected void btnSent_Click(object sender, EventArgs e)
        {
            string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.AddItem);
            Response.Redirect(url);
        }
        protected void btnTim_Click(object sender, EventArgs e)
        {
            GridDataBinding();
        }

        protected static string convertDateToDisplay(string date)
        {
            if (date.Length == 0) return "";
            return date.Substring(0,date.IndexOf(" "));            
        }

        protected void gvItem_PageIndexChanged(object sender, EventArgs e)
        {
            gvItem.SelectedIndex = -1;
        }
    }
}